<?php
header('content-type:text/html;charset=utf-8');
	class MemberControl extends Control {
		
		public function __construct(){
			parent::__construct();
			
			if(!isset($_SESSION['identity'])||$_SESSION['identity']!==$_SERVER['REMOTE_ADDR']){
				$this->error('/index.php/Home/Login/login','您还没有登录，请先登录。');
				exit;
			}
		}
		
		public function index(){
			if(!$this->view->isCached('goods.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
				$orders=new OrdersModel();
				$ordersData=$orders->order_list($_SESSION['uid']);
				
				$this->view->assign('ordersData',$ordersData);
			}
			$this->view->display('index.tpl');
		}
		
		public function buy(){
			$id=$_GET['id'];
			$goods=new GoodsModel();
			$goodsData=$goods->selGoods($id);
			
			$cart=new Cart();
			$cart->goodsID=$id;
			$cart->name=$goodsData['name'];
			$cart->photo=$goodsData['photo'];
			$cart->price=$goodsData['price'];
			$cart->market=$goodsData['market'];
			$cart->num=$_GET['num'];
			$cart->add();
			$this->jump('/index.php/Home/Member/cart');
		}
		public function ajaxbuy(){
			if($_POST['flag']=='add'){
				$goodsID=$_POST['goodsID'];
				for($i=0;$i<count($_SESSION['Cart']);$i++){
					if($_SESSION['Cart'][$i]['goodsID']==$goodsID){
						$_SESSION['Cart'][$i]['num']+=1;
						echo json_encode($_SESSION['Cart'][$i]);
						exit;
					}
				}
			}elseif($_POST['flag']=='decr'){
				$goodsID=$_POST['goodsID'];
				for($i=0;$i<count($_SESSION['Cart']);$i++){
					if($_SESSION['Cart'][$i]['goodsID']==$goodsID){
						if($_SESSION['Cart'][$i]['num']>0){
							$_SESSION['Cart'][$i]['num']-=1;
							echo json_encode($_SESSION['Cart'][$i]);
							exit;
						}
					}
				}
			}
		}
		public function cart(){
			//公共部分
			
			$str=file_get_contents(CONFIG_DIR.'/public.conf');
			eval($str);
			$this->view->caching=false;
			$marketTotal=0;
			if(isset($_SESSION['Cart'])){
				foreach($_SESSION['Cart'] as $val){
					$marketTotal+=$val['num']*$val['market'];
				}
				
			}
			$this->view->assign('total',0);
			$this->view->assign('marketTotal',$marketTotal);
			$this->view->display('cart.tpl');
		}
		
		public function delcart(){
			$id=$_GET['id'];
			$cart=new Cart();
			$cart->goodsID=$id;
			$cart->remove();
			$this->jump($_SERVER['HTTP_REFERER']);
		}
		
		public function cart_checkout(){
			if(!$this->view->isCached('cart_checkout.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			}
			if(isset($_SESSION['Cart'])){
				$total=0;
				foreach ($_SESSION['Cart'] as $val){
					$total+=$val['num']*$val['price'];
				}
				$this->view->assign('Cart',$_SESSION['Cart']);
				$this->view->assign('total',$total);
			}
			$this->view->caching=false;
			$this->view->display('cart_checkout.tpl');
			
		}
		/**
		 * 购物车信息存入数据库，未完善
		 *
		 */
		public function orders(){
			$cartinfo=$_SESSION['Cart'];
			$time=time();
			$amount=0;
			$orderid=date('YmdHis',$time).str_replace('.','',$_SERVER['REMOTE_ADDR']);
			$orderslistModel=new OrderslistModel();
			$ordersModel=new OrdersModel();
			$goodssale=new GoodsModel();
			foreach ($cartinfo as $val){
				$amount+=$val['price']*$val['num'];
				
				$orderslist=array(
					'orderid'=>$orderid,
					'goodsID'=>$val['goodsID'],
					'price'	=>$val['price'],
					'num'	=>$val['num'],
					'express'=>$_POST['express'],
					'payment'=>$_POST['payment'],
					'postscript'=>$_POST['postscript']
				);
				$goodsarray=array(
					'sales'=>$val['num'],
					'id'=>$val['goodsID']
				);
				$goodssale->update($goodsarray);
				$num=$orderslistModel->insert($orderslist);
				if($num!=1){
					die('插入失败'.mysql_error());
				}
			}
			$orders=array(
				'orderid'	=>$orderid,
				'mid'		=>$_SESSION['uid'],
				'btime'		=>$time,
				'amount'	=>$amount,
				'status'	=>0,
				'address'	=>$_SESSION['address']
			);
			$num=$ordersModel->insert($orders);
			if($num==1){
				unset($_SESSION['Cart']);
				$this->jump('/index.php/Home/Member/buydone/orderid/'.$orderid);
			}else{
				$this->error('/index.php/Home/Member/cart');
			}
		}
		
		public function buydone(){
			$orderid=$_GET['orderid'];
			if(!$this->view->isCached('buydone.tpl',$orderid)){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
				
				$orderslist=new OrderslistModel();
				$orders=new OrdersModel();
				$ordersData=$orders->ordersbuydone($orderid);
				$orderslistData=$orderslist->orderslistbuydone($orderid);
				$this->view->assign('express',$orderslistData[0]['express']);
				$this->view->assign('payment',$orderslistData[0]['payment']);
				$this->view->assign('ordersData',$ordersData);
				$this->view->assign('orderslistData',$orderslistData);
			}
			$this->view->display('buydone.tpl',$orderid);
		}
		
/*************************************************************************************/
		/**
		 * 用户信息
		 */
		public function profile(){
			if(!$this->view->isCached('goods.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			
				$member=new MemberModel();
				$memberData=$member->fetchRow($_SESSION['uid']);
				$this->view->assign('memberData',$memberData);
			}
			$this->view->display('profile.tpl');
		}
		/**
		 * 修改密码
		 */
		public function editpwd(){
			if(!$this->view->isCached('editpwd.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			}
			$this->view->display('editpwd.tpl');
			
		}
		/**
		 * 收获地址
		 */
		public function address_list(){
			if(!$this->view->isCached('address_list.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			}
			
			$this->view->display('address_list.tpl');
		}
		/**
		 * 我的订单
		 */
		public function order_list(){
			if(!$this->view->isCached('index.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			
				$orders=new OrdersModel();
				$ordersData=$orders->order_list($_SESSION['uid']);
				$this->view->assign('ordersData',$ordersData);
			}
			$this->view->display('index.tpl');
		}
		/**
		 * 详单
		 *
		 */
		public function orderdetail(){
			$orderid=$_GET['orderid'];
			if(!$this->view->isCached('ordersdetail.tpl',$orderid)){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			
				$orderslist=new OrderslistModel();
				$orderslistData=$orderslist->orderlistdetail($orderid);
					
				$orders=new OrdersModel();
				$ordersData=$orders->ordersbuydone($orderid);
				
				$user=new MemberModel();
				$userData=$user->fetchRow($ordersData['mid']);
				
				$this->view->assign('orderslistData',$orderslistData);
				$this->view->assign('ordersData',$ordersData);
				$this->view->assign('userData',$userData);
			}
			$this->view->display('ordersdetail.tpl',$orderid);
		}
		
		/**
		 * 我的留言
		 */
		public function message_list(){
			if(!$this->view->isCached('message_list.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			
				$message=new MessageModel();
				$messageData=$message->fetchmessage($_SESSION['uid']);
				$this->view->assign('messageData',$messageData);
			}
			$this->view->display('message_list.tpl');
		}
		public function messageadd(){
			if(!$this->view->isCached('messageadd.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			
				$cateData=$cate->fetchAll();
				$cateData=pidsort($cateData);
				$this->view->assign('cateData',$cateData);
			}
			$this->view->display('messageadd.tpl');
		}
		public function messageaddck(){
			
			$_POST['mtime']=time();
			$_POST['uid']=$_SESSION['uid'];
			$_POST['username']=$_SESSION['username'];
			$message=new MessageModel();
			$num=$message->insert($_POST);
			if($num==1){
				$this->success('/index.php/Home/Member/message_list');
			}else{
				$this->error('/index.php/Home/Member/message_list');
			}
		}
		/**
		 * 我的收藏
		 */
		public function collection_list(){
			if(!$this->view->isCached('collection.tpl')){
				//公共部分
				$str=file_get_contents(CONFIG_DIR.'/public.conf');
				eval($str);
			}
			
			$this->view->display('collection_list.tpl');
		}
		
		public function infoupdate(){
			if(isset($_POST['pwd'])){
				$_POST['pwd']=md5($_POST['pwd']);
			}
			$member=new MemberModel();
			$num=$member->update($_POST);
			
			if($num==1){
				$_SESSION['relname']=isset($_POST['relname'])?$_POST['relname']:$_SESSION['relname'];
				$_SESSION['address']=isset($_POST['address'])?$_POST['address']:$_SESSION['address'];
				$_SESSION['phone']=isset($_POST['phone'])?$_POST['phone']:$_SESSION['phone'];
				$_SESSION['code']=isset($_POST['code'])?$_POST['code']:$_SESSION['code'];
				$_SESSION['email']=isset($_POST['email'])?$_POST['email']:$_SESSION['email'];
				
				$this->success('/index.php/Home/Member/index');
			}else{
				$this->error('/index.php/Home/Member/index',mysql_error());
			}
		}
	}
	
?>